package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

@Component
public class ConverterFactorys implements ConverterFactory<String, BaseEnum> {
    @Override
    //<T extends BaseEnum> 声明类型   Converter<String, T> 返回类型是一个Converter 方法参数必须XXX.class的
    public <T extends BaseEnum> Converter<String, T> getConverter (Class<T> targetType) {
        return new Converter<String, T>() {
            @Override
            public T convert(String code) {
                //获取枚举数组
                T[] arr = targetType.getEnumConstants();
                //遍历枚举数组给s
                for (T s : arr) {
                    //枚举的状态码和传进来的作比较看是否一样
                    s.getCode().equals(Integer.valueOf(code));
                    return s;
                }
                throw new RuntimeException("没有找到对应的枚举");

            }
        };
    }
}
